package me.kafeitu.demo.activiti.cmd;

/**
 * @author: Henry Yan
 */

import java.util.List;

import org.flowable.bpmn.model.Activity;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

public class JumpActivityCmd implements Command<Object> {
    private String activityId;
    private String processInstanceId;
    private String jumpOrigin;

    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();  
    
    public JumpActivityCmd(String processInstanceId, String activityId) {
        this(processInstanceId, activityId, "jump");
    }

    public JumpActivityCmd(String processInstanceId, String activityId, String jumpOrigin) {
        this.activityId = activityId;
        this.processInstanceId = processInstanceId;
        this.jumpOrigin = jumpOrigin;
    }

    public Object execute(CommandContext commandContext) {
    	TaskService taskService = processEngine.getTaskService();
    	
    	ProcessInstance pInstance= processEngine.getRuntimeService()
                .createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
    	
    	
    	BpmnModel model = ProcessDefinitionUtil.getBpmnModel(pInstance.getProcessDefinitionId());
    	
    	
        //ExecutionEntity executionEntity = mng.findByRootProcessInstanceId(processInstanceId);

        //executionEntity.destroyScope(jumpOrigin);
      
        ProcessDefinition pd = null;
        // Activity activity = pd.findActivity(activityId);

        //executionEntity.executeActivity(activity);
      

        Task task = taskService.createTaskQuery().taskId(jumpOrigin).singleResult();

        taskService.complete(jumpOrigin);
        
        //return executionEntity;
        return commandContext.getResult();
    }
}
